package net.asfun.jvalog.service;

import java.util.ArrayList;
import java.util.List;

import net.asfun.jvalog.common.JdoerFactory;
import net.asfun.jvalog.entity.Paper;
import net.asfun.jvalog.resource.PaperDao;
import net.asfun.jvalog.util.Unifier;
import net.asfun.jvalog.util.UpdateManager;
import net.asfun.jvalog.vo.Page;
import net.asfun.jvalog.vo.Pager;


import com.google.appengine.api.datastore.Key;

public class PageService {
	

	public static List<Page> listPublished() {
		List<Page> pages = new ArrayList<Page>();
		for(Paper paper : PaperDao.listPublished(Paper.PAGE_TYPE) ) {
			pages.add(new Page(paper));
		}
		return pages;
	}

	public static Page getBySlogan(String slogan) {
		return new Page(PaperDao.getBySlogan(slogan));
	}

	public static List<Page> listAll(Pager pager) {
		List<Page> pages = new ArrayList<Page>();
		for(Paper paper : PaperDao.listAll(Paper.PAGE_TYPE, pager) ) {
			pages.add(new Page(paper));
		}
		return pages;
	}

	public static void addPage(Paper paper) {
		paper.setSlogan(uniqueURI(paper.getSlogan()));
		paper.setUri(generateURI(paper.getSlogan()));
		JdoerFactory.jdoPaper.save(paper);
		Unifier.setUnique(paper.getKey(), "slogan", paper.getSlogan());
		if ( paper.isPublished() ) {
			UpdateManager.enableStaticTask();
		}
	}

	private static String uniqueURI(String slogan) {
		if ( ! Unifier.isUnique("Paper", "slogan", slogan)) {
			slogan += "~1";
			return slogan = uniqueURI(slogan);
		}
		return slogan;	
	}
	
	private static String uniqueURI(String slogan, Key key) {
		if ( ! Unifier.isUnique(key, "slogan", slogan)) {
			slogan += "~1";
			return slogan = uniqueURI(slogan, key);
		}
		return slogan;	
	}
	
	private static String generateURI(String slogan) {
		return "page/" + slogan;
	}

	public static void delete(Long id) {
		Paper paper = JdoerFactory.jdoPaper.load(id);
		if ( paper != null ) {
			if ( paper.isPublished() ) {
				UpdateManager.enableStaticTask();
			}
			Unifier.unsetUnique(paper.getKey(), "slogan");
			JdoerFactory.jdoPaper.delete(paper);
		}
	}

	public static void update(Paper paper) {
		Paper record = JdoerFactory.jdoPaper.load(paper.getKey().getId());
		boolean isPublished = record.isPublished();
		if ( paper.getTitle() != null )
			record.setTitle(paper.getTitle());
		if ( paper.getExcerpt() != null )
			record.setExcerpt(paper.getExcerpt());
		record.setPublished(paper.isPublished());
		if ( paper.getSlogan() != null )
			record.setSlogan(uniqueURI(paper.getSlogan(), record.getKey()));
		if ( paper.getSource() != null )
			record.setSource(paper.getSource());
		if ( paper.getContent() != null )
			record.setContent(paper.getContent());
		if ( paper.getDate() != null )
			record.setDate(paper.getDate());
		record.setUri(generateURI(record.getSlogan()));
		
		Unifier.setUnique(paper.getKey(), "slogan", paper.getSlogan());
		
		if ( isPublished || record.isPublished() ) {
			UpdateManager.enableStaticTask();
		}
	}

	public static Page getById(Long id) {
		return new Page(JdoerFactory.jdoPaper.load(id));
	}
}
